//
//  MineViewController.m
//  0.3camera
//
//  Created by 茶 on 2023/11/24.
//

#import "MineViewController.h"
#import "MIneTableViewCell.h"

@interface MineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_tableView reloadData];
}

- (void)initSubView {
    self.view.backgroundColor = HexColor(@"#F8F8F8");
    self.title = @"我的";
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = HexColor(@"#F8F8F8");
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.estimatedRowHeight = 0;
    _tableView.rowHeight = CARatioWidth(70);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.bounces = NO;
    [_tableView registerClass:MIneTableViewCell.class forCellReuseIdentifier:kMineCellIdentifier];
    [self.view addSubview:_tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MIneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineCellIdentifier forIndexPath:indexPath];
    NSString *title = self.titleArray[indexPath.row];
    if ([title isEqualToString:@"会员享用所有功能"]) {
        cell.type = 0;
    } else {
        cell.type = 1;
    }
    cell.title = title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titleArray[indexPath.row];
    
    if ([title isEqualToString:@"会员享用所有功能"]) {
        [self.navigationController pushViewController:[NSClassFromString(@"CAVipViewController") new] animated:YES];
    } else if ([title isEqualToString:@"给个好评"]){
        NSString * nsStringToOpen = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/app/id%@?action=write-review", @"6472980002"];//替换为对应的APPID
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:nsStringToOpen] options:@{} completionHandler:nil];
    } else if ([title isEqualToString:@"联系官方"]){
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = KWeChat;
        [self.view makeToast:@"已复制客服号码，请前往微信添加"];
        
        UIImpactFeedbackGenerator *generator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
        [generator impactOccurred];
        
    } else if ([title isEqualToString:@"隐私政策"]){
        [UIApplication.sharedApplication openURL:[NSURL URLWithString:kPrivacy] options:@{} completionHandler:nil];

    } else if ([title isEqualToString:@"用户协议"]){
        [UIApplication.sharedApplication openURL:[NSURL URLWithString:kAgreement] options:@{} completionHandler:nil];

    }
}

- (NSArray *)titleArray {
    if (_titleArray) return _titleArray;
    _titleArray = @[@"会员享用所有功能", @"给个好评", @"联系官方", @"隐私政策", @"用户协议"];
    return _titleArray;
}
@end
